<template>
  <div>
    <el-form :inline="true" v-show="isSearchCollapse" class="query-form" ref="searchForm" :model="searchForm"
      @keyup.enter.native="refreshList()" @submit.native.prevent>
      <!-- 搜索框-->
      <el-form-item prop="rfid">
        <el-input size="small" v-model="searchForm.rfid" placeholder="工牌ID" clearable></el-input>
      </el-form-item>
      <el-form-item prop="devEui">
        <el-input size="small" v-model="searchForm.devEui" placeholder="devEui" clearable></el-input>
      </el-form-item>
      <el-form-item prop="cardType">
        <el-input size="small" v-model="searchForm.cardType" placeholder="工牌类型" clearable></el-input>
      </el-form-item>
      <!-- <el-form-item prop="cardType">
        <el-select v-model="searchForm.cardType" placeholder="请选择工牌类型" size="small" style="width: 100%;">
          <el-option v-for="item in $dictUtils.getDictList('lora_rfid_type')" :key="item.value" :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
        <el-button @click="resetSearch()" size="small">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 导入导出-->
    <el-form :inline="true" v-show="isImportCollapse" class="query-form" ref="importForm">
      <el-form-item>
        <el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
      </el-form-item>
      <el-form-item prop="loginName">
        <el-upload class="upload-demo" :action="`${this.$http.BASE_URL}/lora/rfid/jfLoraRfid/import?projectId=`+$store.state.config.projectId"
          :on-success="uploadSuccess" :show-file-list="true">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
        </el-upload>
      </el-form-item>
    </el-form>
    <el-row>
      <el-button v-if="hasPermission('lora:rfid:jfLoraRfid:add')" type="primary" size="small" icon="el-icon-plus"
        @click="add()">新建</el-button>
<!--      <el-button v-if="hasPermission('lora:rfid:jfLoraRfid:edit')" type="warning" size="small"
        icon="el-icon-edit-outline" @click="edit()" :disabled="dataListSelections.length != 1" plain>修改</el-button> -->
      <el-button v-if="hasPermission('lora:rfid:jfLoraRfid:edit')" type="success" size="small"
        icon="el-icon-edit-outline" @click="param()" :disabled="dataListSelections.length <= 0" plain>参数配置</el-button>
      <el-button v-if="hasPermission('lora:rfid:jfLoraRfid:del')" type="danger" size="small" icon="el-icon-delete"
        @click="del()" :disabled="dataListSelections.length <= 0" plain>删除
      </el-button>
      <el-button-group class="pull-right">
        <el-button type="default" size="small" icon="el-icon-search"
          @click="isSearchCollapse = !isSearchCollapse, isImportCollapse=false">
        </el-button>
        <el-button v-if="hasPermission('lora:rfid:jfLoraRfid:import')" type="default" size="small"
          icon="el-icon-upload2" title="导入" @click="isImportCollapse = !isImportCollapse, isSearchCollapse=false">
        </el-button>
        <el-button v-if="hasPermission('lora:rfid:jfLoraRfid:export')" type="default" size="small"
          icon="el-icon-download" title="导出" @click="exportExcel()"></el-button>
        <el-button type="default" size="small" icon="el-icon-refresh" @click="refreshList">
        </el-button>
      </el-button-group>
    </el-row>
    <el-table :data="dataList" border size="medium" @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle" v-loading="loading" class="table">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="rfid" show-overflow-tooltip sortable="custom" label="工牌ID">
        <template slot-scope="scope">
          <el-link type="primary" :underline="false" v-if="hasPermission('lora:rfid:jfLoraRfid:edit')"
            @click="edit(scope.row.id)">{{scope.row.rfid}}</el-link>
          <el-link type="primary" :underline="false" v-else-if="hasPermission('lora:rfid:jfLoraRfid:view')"
            @click="view(scope.row.id)">{{scope.row.rfid}}</el-link>
          <span v-else>{{scope.row.rfid}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="devEui" min-width="130px" show-overflow-tooltip sortable="custom" label="devEui">
      </el-table-column>
      <el-table-column prop="rx" min-width="130px" show-overflow-tooltip sortable="custom" label="rx">
      </el-table-column>
      <el-table-column prop="uuid" min-width="130px" show-overflow-tooltip sortable="custom" label="uuid">
      </el-table-column>
      <el-table-column prop="cardType"  header-align="center" align="center" min-width="90px" show-overflow-tooltip sortable="custom" label="工牌类型">
        <!-- <template slot-scope="scope">
          {{ $dictUtils.getDictLabel("lora_rfid_type", scope.row.cardType, '-') }}
        </template> -->
      </el-table-column>
      <el-table-column prop="power" header-align="center" align="center" min-width="50px" show-overflow-tooltip sortable="custom" label="电量">
        <template slot-scope="scope">
          {{ scope.row.power || scope.row.power===0  ? scope.row.power+'%' : '-' }}
        </template>
      </el-table-column>
      <el-table-column prop="userId"  header-align="center" align="center" show-overflow-tooltip sortable="custom" label="使用状态">
        <template slot-scope="scope">
          <el-tag key="已使用" type="success" v-if="scope.row.userId && scope.row.userId !== ''">已使用</el-tag>
          <el-tag key="未使用" type="info" v-else>未使用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="onlineTime"  header-align="center" align="center" show-overflow-tooltip sortable="custom" label="工牌状态">
        <template slot-scope="scope">
          <el-tag key="在线" type="success" v-if="scope.row.isOnLine">在线</el-tag>
          <el-tag key="离线" type="info" v-else>离线</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="onlineTime"  header-align="center" align="center" show-overflow-tooltip sortable="custom" label="位置上传">
        <template slot-scope="scope">
          <el-tag key="有效" type="success" v-if="scope.row.posOnLine">有效</el-tag>
          <el-tag key="无效" type="info" v-else>无效</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="versionDev"  header-align="center" align="center" show-overflow-tooltip sortable="custom" label="版本号">
        <template slot-scope="scope">
          <div v-if="scope.row.versionDev === scope.row.versionNew">{{scope.row.versionDev}}</div>
          <div v-else style="font-size: 12px;color: #F5222D;">更新中...</div>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="onlineTime" show-overflow-tooltip sortable="custom" label="在线状态">
      </el-table-column> -->
      <!-- <el-table-column prop="versionDev" show-overflow-tooltip sortable="custom" label="设备上参数版本">
      </el-table-column>
      <el-table-column prop="versionNew" show-overflow-tooltip sortable="custom" label="需要更新参数版本">
      </el-table-column> -->
      <el-table-column header-align="center" align="center" fixed="right" width="220" label="操作">
        <template slot-scope="scope">

         <!-- <el-button v-if="hasPermission('smis:system')" type="text" icon="el-icon-guide" size="small"
            @click="animation(scope.row)">动画</el-button> -->
          <el-button v-if="hasPermission('lora:rfid:jfLoraRfid:view')" type="text" icon="el-icon-view" size="small"
            @click="view(scope.row.id)">查看</el-button>
          <el-button v-if="hasPermission('lora:rfid:jfLoraRfid:edit')" type="text" icon="el-icon-setting" size="small"
            @click="param(scope.row.id)">工牌参数</el-button>
          <!-- <el-button v-if="hasPermission('lora:rfid:jfLoraRfid:edit')" type="text" icon="el-icon-edit" size="small"
            @click="edit(scope.row.id)">修改</el-button> -->
          <el-button v-if="hasPermission('lora:rfid:jfLoraRfid:del')" type="text" icon="el-icon-delete" size="small"
            @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100, 500, 1000, 2000]" :page-size="pageSize" :total="total" background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <JfLoraRfidForm ref="jfLoraRfidForm" @refreshDataList="refreshList"></JfLoraRfidForm>
    <!-- 弹窗, 参数修改 -->
    <JfLoraRfidParamForm ref="jfLoraRfidParamForm" @refreshDataList="refreshList"></JfLoraRfidParamForm>
  </div>
</template>

<script>
  import JfLoraRfidForm from './JfLoraRfidForm'
  import JfLoraRfidParamForm from './JfLoraRfidParamForm'
  export default {
    data() {
      return {
        searchForm: {
          rfid: '',
          devEui: '',
          cardType: ''
        },
        dataList: [],
        pageNo: 1,
        pageSize: 10,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isSearchCollapse: false,
        isImportCollapse: false,
        loading: false
      }
    },
    components: {
      JfLoraRfidForm,
      JfLoraRfidParamForm
    },
    activated() {
      this.refreshList()
    },
    computed: {
      onlineTime: {
        get () {
          if (!this.$store.state.config.backParam.onlineTime){
            return 1200
          }
          return this.$store.state.config.backParam.onlineTime
        }
      }
    },
    methods: {
      dateToState (value) {
        if (value) {
          return ((new Date()).getTime() - value) < this.onlineTime*1000 ? true : false
        } else {
          return false
        }
      },
      // 获取数据列表
      refreshList() {
        this.loading = true
        this.$http({
          url: '/lora/rfid/jfLoraRfid/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            ...this.searchForm
          }
        }).then(({
          data
        }) => {
          if (data && data.success) {
            this.dataList = data.page.list
            this.total = data.page.count
            this.loading = false
            for(var i=0; i<this.dataList.length; i++) {
              this.dataList[i].isOnLine = this.dateToState(this.dataList[i].onlineTime)
              this.dataList[i].posOnLine = this.dateToState(this.dataList[i].posUpdateTime)
            }
          }
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },

      // 排序
      sortChangeHandle(obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 动画演示
      animation(row) {
        var points = [
          [-47.564583256621276,-17.29030408935813],
          [-47.40316513121306,-20.499505621440846],
          [-47.77982549459891,-25.264772158041524],
          [-47.45567647908452,-25.579879169961274],
          [-47.83036568835887,-30.2672826326312],
          [-47.30267643668628,-34.60231493426543],
          [-47.12158701733814,-44.02975221738855],
          [-47.77363605845939,-54.39748883628863],
          [-53.3889815936768,-54.6973838539878],
          [-58.723471080892644,-54.70489658184227],
          [-64.35619522110116,-54.68801951182408],
          [-66.4545659354489,-50.58356397368294],
          [-66.08702230174052,-47.62113927490375],
          [-66.08603270911601,-43.735205221525106],
          [-66.94498371150299,-40.4165866925985],
          [-66.34113679471818,-36.454241388586176],
          [-65.89420363532825,-30.277045989512544],
          [-65.82169683737308,-27.150751589363054],
          [-66.55830735836574,-17.29592357980576],
          [-66.27079695573849,-4.197766055978377],
          [-66.12422106635262,-1.8599198566047903],
          [-66.27072632476906,4.555187488835443],
          [-66.46943799855582,11.767456578070181],
          [-66.1956033829893,16.99991796243249],
          [-66.29577022043489,23.012180238295386],
          [-66.67856207379704,33.11108453001937],
          [-65.9629207274471,37.14835225661345],
          [-66.69815476985357,43.20033136527752],
          [-63.19625451537286,49.20740810882839],
          [-59.647743896436864,49.09930426075255],
          [-56.439406206801024,49.29269510015499],
          [-47.75755530701481,49.49401659470465],
          [-36.14952227888695,49.2277223025977],
          [-28.430018154107344,49.37088951826115],
          [-22.63567125356893,49.39815925684829],
          [-14.27465424452518,49.58014166529421],
          [-8.480307381617237,49.60741140349535],
          [-4.610971913639389,49.51879245721105],
          [-0.49815017869742917,50.732701853324954],
          [1.1561054861952584,55.34040924152808]]
        var timer = setInterval(()=>{
          if(points.length === 0) {
            clearInterval(timer);
          } else {
            var point = points.shift();
            row.x = point[0];
            row.y = point[1];
            row.floorId = '42'
            row.onlineTime = (new Date()).getTime()
            console.log(row)
            this.$http({
              url: `/lora/rfid/jfLoraRfid/save`,
              method: 'post',
              data: row
            }).then(({
              data
            }) => {
            })
          }
        }, 4000)
      },
      // 新增
      add() {
        this.$refs.jfLoraRfidForm.init('add', '')
      },
      // 修改
      edit(id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.jfLoraRfidForm.init('edit', id)
      },
      // 工牌参数
      param(id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$refs.jfLoraRfidParamForm.init('edit', id)
      },
      // 查看
      view(id) {
        this.$refs.jfLoraRfidForm.init('view', id)
      },
      // 删除
      del(id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/lora/rfid/jfLoraRfid/delete',
            method: 'delete',
            params: {
              'ids': ids
            }
          }).then(({
            data
          }) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 导入成功
      uploadSuccess(res, file) {
        if (res.success) {
          this.$message.success({
            dangerouslyUseHTMLString: true,
            message: res.msg
          })
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl() {
        this.$utils.download('/lora/rfid/jfLoraRfid/import/template')
      },
      exportExcel() {
        this.$utils.download('/lora/rfid/jfLoraRfid/export')
      },
      resetSearch() {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>
